//
//  BSZTabBarController.m
//  BuSuanZi
//
//  Created by staff on 3/15/25.
//

#import "BSZTabBarController.h"
#import "BSZTabBar.h"

#import "BSZHomeViewController.h"
#import "BSZMineViewController.h"

#import "BSZNavigationController.h"

@interface BSZTabBarController ()<UITabBarControllerDelegate>

@property (nonatomic, strong) BSZTabBar *zdyTabBar;
@property (nonatomic, strong) BSZNavigationController *prevNav;

@property (nonatomic,strong) NSMutableArray  *tabbarButtonArry;

@end

@implementation BSZTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.delegate = self;
            
    [self setupMainTabBar];
    [self setupAllControllers];
}

#pragma mark --初始化
- (void)setupMainTabBar{
    BSZTabBar *mainTabBar = [[BSZTabBar alloc] init];
    //设置背景颜色不透明
    self.zdyTabBar.translucent = NO;
    [self setValue:mainTabBar forKeyPath:@"tabBar"];
    self.zdyTabBar = mainTabBar;
//    [self.zdyTabBar.centerBtn addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    
    [self.tabBar setBackgroundImage:[UIImage new]];
    [self.tabBar setShadowImage:[UIImage new]];
    [self dropShadowWithOffset:CGSizeMake(0, -1) radius:0 color:LineColor opacity:0.5];
    
    // 方式三：适配iOS13
    if (@available(iOS 13.0, *)) {
            // iOS13 及以上
            self.tabBar.tintColor = DefaultColor;
            self.tabBar.unselectedItemTintColor = LightGrayColor;
     }
    else {
           // iOS13 以下
           UITabBarItem *item = [UITabBarItem appearance];
           [item setTitleTextAttributes:@{ NSForegroundColorAttributeName:LightGrayColor} forState:UIControlStateNormal];
           [item setTitleTextAttributes:@{ NSForegroundColorAttributeName:DefaultColor} forState:UIControlStateSelected];
     }
}

#pragma mark --初始化控制器
-(void)setupAllControllers{
    BSZHomeViewController *homeVc = [[BSZHomeViewController alloc]init];
    BSZNavigationController *homeNav = [self addChildVc:homeVc title:@"首页" image:@"tab_home_n" selectedImage:@"tab_home_s" tag:0];
    [self addChildViewController:homeNav];

    BSZMineViewController *myVc = [[BSZMineViewController alloc]init];
    BSZNavigationController *myNav = [self addChildVc:myVc title:@"我的" image:@"tab_mine_n" selectedImage:@"tab_mine_s" tag:4];
    [self addChildViewController:myNav];
}
#pragma mark - 添加子控制器  设置图片
- (BSZNavigationController *)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage tag:(NSInteger)tag{
    // 设置子控制器的文字
    childVc.title = title; // 同时设置tabbar和navigationBar的文字
    
    childVc.tabBarItem.tag = tag;
    // 设置子控制器的图片
    childVc.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    //未选中字体颜色  system为系统字体
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:LightGrayColor,NSFontAttributeName:SystemFont(12.0)} forState:UIControlStateNormal];
    //选中字体颜色
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:DefaultColor,NSFontAttributeName:SystemFont(12.0)} forState:UIControlStateSelected];

    // 先给外面传进来的小控制器 包装 一个导航控制器
    BSZNavigationController *nav = [[BSZNavigationController alloc] initWithRootViewController:childVc];
    return nav;
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    [self.tabbarButtonArry removeAllObjects];
    
    for (UIView *tabBarButton in self.tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [self.tabbarButtonArry addObject:tabBarButton];
        }
    }
    
    for (NSInteger i=0; i<self.tabbarButtonArry.count; i++) {
        if (i == self.selectedIndex) {
            UIView *tabBarButton = self.tabbarButtonArry[i];
            
            for (UIView *imageView in tabBarButton.subviews) {
                if ([imageView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
                    CABasicAnimation*pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
                        pulse.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
                        pulse.duration = 0.15;
                        pulse.repeatCount= 1;
                        pulse.autoreverses= YES;
                        pulse.fromValue= [NSNumber numberWithFloat:0.7];
                        pulse.toValue= [NSNumber numberWithFloat:1.3];
                    [imageView.layer addAnimation:pulse forKey:nil];
                }
                break;
            }
            break;
        }
    }
}

//tabBar顶部加阴影
- (void)dropShadowWithOffset:(CGSize)offset
                      radius:(CGFloat)radius
                       color:(UIColor *)color
                     opacity:(CGFloat)opacity {
    
    // Creating shadow path for better performance
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, self.zdyTabBar.bounds);
    self.tabBar.layer.shadowPath = path;
    CGPathCloseSubpath(path);
    CGPathRelease(path);
    
    self.zdyTabBar.layer.shadowColor = color.CGColor;
    self.zdyTabBar.layer.shadowOffset = offset;
    self.zdyTabBar.layer.shadowRadius = radius;
    self.zdyTabBar.layer.shadowOpacity = opacity;
    
    // Default clipsToBounds is YES, will clip off the shadow, so we disable it.
    self.zdyTabBar.clipsToBounds = NO;
}

- (NSMutableArray *)tabbarButtonArry{
    if (!_tabbarButtonArry) {
        _tabbarButtonArry = [NSMutableArray arrayWithCapacity:0];
    }
    return _tabbarButtonArry;
}

//                for (UIControl *tabBarButtonLabel in tabBarButton.subviews) {
//                    if ([tabBarButtonLabel isKindOfClass:NSClassFromString(@"UITabBarButtonLabel")]) {
//                        UILabel *label = (UILabel *)tabBarButtonLabel;
//                        if ([label.text isEqualToString:@"首页"] || [label.text isEqualToString:@"动态"] || [label.text isEqualToString:@"减重名医"] || [label.text isEqualToString:@"商城"] || [label.text isEqualToString:@"我的"]) {
//                            for (UIView *imageView in tabBarButton.subviews) {
//
//                            if ([imageView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
//                                }
//                            }
//                        }
//                    }
//                }

#pragma mark --落地动画
//                                    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.y"];
//                                    //通过初中物理重力公式计算出的位移y值数组
//                                    animation.values = @[@0.0, @-4.15, @-7.26, @-9.34, @-10.37, @-9.34, @-7.26, @-4.15, @0.0, @2.0, @-2.9, @-4.94, @-6.11, @-6.42, @-5.86, @-4.44, @-2.16, @0.0];
//                                    animation.duration = 0.8;
//                                    animation.beginTime = CACurrentMediaTime() + 1;
//                                    [imageView.layer addAnimation:animation forKey:nil];

#pragma mark --放大缩小
//CABasicAnimation*pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
//    pulse.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//    pulse.duration = 0.08;
//    pulse.repeatCount= 1;
//    pulse.autoreverses= YES;
//    pulse.fromValue= [NSNumber numberWithFloat:0.7];
//    pulse.toValue= [NSNumber numberWithFloat:1.3];
//[imageView.layer addAnimation:pulse forKey:nil];

//放大效果
//CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
////速度控制函数，控制动画运行的节奏
//animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//animation.duration = 0.2;       //执行时间
//animation.repeatCount = 1;      //执行次数
//animation.removedOnCompletion = NO;
//animation.fillMode = kCAFillModeForwards;           //保证动画效果延续
//animation.fromValue = [NSNumber numberWithFloat:1.0];   //初始伸缩倍数
//animation.toValue = [NSNumber numberWithFloat:1.15];     //结束伸缩倍数
//[[arry[index] layer] addAnimation:animation forKey:nil];
////移除其他tabbar的动画
//for (int i = 0; i<arry.count; i++) {
//    if (i != index) {
//        [[arry[i] layer] removeAllAnimations];
//    }
//}

//z轴旋转180度
//CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
////速度控制函数，控制动画运行的节奏
//animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//animation.duration = 0.2;       //执行时间
//animation.repeatCount = 1;      //执行次数
//animation.removedOnCompletion = YES;
//animation.fromValue = [NSNumber numberWithFloat:0];   //初始伸缩倍数
//animation.toValue = [NSNumber numberWithFloat:M_PI];     //结束伸缩倍数
//[[arry[index] layer] addAnimation:animation forKey:nil];

//向上移动
//CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
////速度控制函数，控制动画运行的节奏
//animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//animation.duration = 0.2;       //执行时间
//animation.repeatCount = 1;      //执行次数
//animation.removedOnCompletion = YES;
//animation.fromValue = [NSNumber numberWithFloat:0];   //初始伸缩倍数
//animation.toValue = [NSNumber numberWithFloat:-10];     //结束伸缩倍数
//[[arry[index] layer] addAnimation:animation forKey:nil];


@end
